package switchviz.graphic.signal;

import javax.swing.JPanel;

import org.jdom.Element;

import switchviz.core.signal.Signal;

public class SimpleSignal extends Signal implements IGaugeSignal
{
	private SimpleSignalDataModel _datamodel = null;
	private SimpleSignalPersistence _persistenceHandler = null;
	private SimpleSignalPanel _testpanel = null;
	
	private boolean _enable = true;

	private long lastTimeStamp = 0;
	private float newValue = 0.0f;
	private float oldValue = 0.0f;
	private float newStep = 0.0f;
	private float oldStep = 0.0f;
	
	private long time = 0;
	
	public SimpleSignal(Element node)
	{
		_persistenceHandler = new SimpleSignalPersistence(node);
		_datamodel = _persistenceHandler.getDataModelFromElement();
		_datamodel.setType(BasicSignalDataModel.TYPESIMPLE);
		this.setAliasName(_datamodel.getAlias());
		this.setPort(_datamodel.getPort());
	}
	
	public SimpleSignal(Element node, String name)
	{
		this(node);
		_datamodel.setName(name);
		_persistenceHandler.setDataModel(_datamodel);
	}
	
	private SimpleSignalPanel getSimplePanel()
	{
		if(_testpanel == null)
			_testpanel = new SimpleSignalPanel(_persistenceHandler.getElement());

		return _testpanel;
	}
	
	public float[] getColor() 
	{
		return _datamodel.getColor();
	}

	public String getName() 
	{
		return _datamodel.getName();
	}

	public float getValue() 
	{
		float value = 0.0f;
		long actualTime = System.currentTimeMillis();

		if (_enable && getData() != null)
		{		
			long timestamp;
				
			if(getData().getValue() != null && lastTimeStamp != (timestamp = getData().getTimeStamp().getValue()))
			{
					if(getData().getValue().getClass().equals(Integer.class))
					{
						value = (Integer)(getData().getValue());
					}
					else if(getData().getValue().getClass().equals(Long.class))
					{
						value = (Long)(getData().getValue());
					}
					
					oldValue = newValue;
					newValue = value;
					oldStep = newStep;
					newStep = (newValue-oldValue)*(1000f/(timestamp-lastTimeStamp)); //always data per seconds 
					
					lastTimeStamp = timestamp;
					time = actualTime;
			}
			else
			{
					value = newValue;
			}
			
			
			if (time != 0)
			{
				if (_datamodel.isDifference())
					value = oldStep + (Math.min(actualTime-time,1000)*(newStep-oldStep)/1000);
				else
					value = oldValue + (Math.min(actualTime-time,1000)*(newValue-oldValue)/1000);
			}
		}
		else
		{
			_enable = false;
			return 0.0f;
		}
		
		return value*_datamodel.getCoefficient() + _datamodel.getOffset();
	}

	public Element getElement() 
	{
		return getSimplePanel().getElement();
	}

	public JPanel getPanel() 
	{
		return getSimplePanel();
	}

	public int getAlarmmode() 
	{
		return _datamodel.getAlarmmode();
	}

	public float getMax() 
	{
		return _datamodel.getMax();
	}

	public float getMin() 
	{
		return _datamodel.getMin();
	}

	public boolean isLogarithmic() 
	{
		return _datamodel.isLogarithm();
	}

	public int getPriority() 
	{
		return _datamodel.getPriority();
	}

	public String getType() 
	{
		return _datamodel.getType();
	}

	public boolean isEnable() 
	{
		return _enable;
	}
}
